package com.music.controller;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.music.common.BaseResult;
import com.music.entity.CmUser;
import com.music.entity.req.CmUserReq;
import com.music.entity.req.VipTime;
import com.music.service.CmUserService;
import com.music.utils.RedisUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * 2 * @Author: 刘佳乐 :面向百度编程
 * 3 * @Date: 2020/12/13 14:30
 * 4
 */
@RestController
@RequestMapping("user")
@Api(description="用户相关接口")
//@CrossOrigin(allowCredentials = "true",allowedHeaders = "*")
public class CmUserController {

    @Autowired
    private CmUserService cmUserService;
    @Autowired
    RedisUtils redisUtils;
    @RequestMapping(value = "findAll",method = RequestMethod.GET)
    public BaseResult findAll(){
        return cmUserService.findAll();
    }

    @RequestMapping(value = "findByTel",method = RequestMethod.POST)
    public BaseResult findByTel(@RequestBody Map map){
        return cmUserService.findByTel( map.get("uTel").toString());
    }

    @RequestMapping(value = "findByuID",method = RequestMethod.POST)
    public BaseResult findByuID(@RequestBody Map map){
        return cmUserService.findByuID((Integer) map.get("uId"));
    }

    @RequestMapping(value = "sendCode",method = RequestMethod.GET)
    public BaseResult sendCode(@RequestParam("uTel") String uTel){
        return cmUserService.sendCode(uTel);
    }

    @RequestMapping(value = "registry",method = RequestMethod.POST)
    public BaseResult registry(@RequestBody CmUserReq cmUserReq){
        return cmUserService.registry(cmUserReq);
    }

    @RequestMapping(value = "login",method = RequestMethod.POST)
    public BaseResult login(@RequestBody CmUser cmUser){
        return cmUserService.login(cmUser);
    }

    @RequestMapping(value = "logout",method = RequestMethod.GET)
    public BaseResult logout(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        String number=null;
        for (Cookie cook:cookies
        ) {
            if (cook.getName().equals("token")){
                number=cook.getValue();
            }
        }
        redisUtils.del(number);
        BaseResult baseResult=new BaseResult();
        baseResult.setMessage("注销成功");
        baseResult.setCode(200);
        return baseResult;
    }

    @RequestMapping(value = "getCmUserInfo",method = RequestMethod.GET)
    public BaseResult getCmUserInfo(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        String number=null;
        for (Cookie cook:cookies
        ) {
            if (cook.getName().equals("token")){
                number=cook.getValue();
            }
        }
        Object o1 = redisUtils.get(number);
        String s = JSONObject.toJSONString(o1);
        CmUser cmUser = JSON.parseObject(s, CmUser.class);
        BaseResult baseResult=new BaseResult();
        baseResult.setData(cmUser);
        baseResult.setCode(200);
        baseResult.setMessage("用户信息已找到");
        return baseResult;
    }


    @RequestMapping(value = "/image1",method = RequestMethod.GET)
    public String productcodeimage1(HttpServletResponse response) throws IOException {
        String s = UUID.randomUUID().toString();
        QrConfig config = new QrConfig(100, 100);
        // 设置边距，既二维码和背景之间的边距
        config.setMargin(1);
        // 设置前景色，既二维码颜色（青色）
        config.setForeColor(Color.BLACK.getRGB());
        // 设置背景色（灰色）
        config.setBackColor(Color.WHITE.getRGB());
        // 生成二维码到文件，也可以到流
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        QrCodeUtil.generate("https://music.163.com/#/download", config, ImgUtil.IMAGE_TYPE_PNG, outputStream);
        byte[] pngData = outputStream.toByteArray();
        // 这个前缀，可前端加，可后端加，不加的话，不能识别为图片
        return "data:image/png;base64," + Base64.encode(pngData);
    }

    @RequestMapping(value = "/openvip",method = RequestMethod.POST)
    public BaseResult openVip(@RequestBody Map map){
        return cmUserService.updatestatus((Integer) map.get("uId"),(Integer) map.get("month"));
    }

    @RequestMapping(value = "/findvipuser",method = RequestMethod.POST)
    public BaseResult findvipuser(@RequestBody Map map){
       return cmUserService.deletestatus((Integer) map.get("uId"));
    }
    @RequestMapping(value = "finAllByFriend",method = RequestMethod.GET)
    public BaseResult finAllByFriend(){
        return cmUserService.finAllByFriend();
    }

    @RequestMapping(value = "/updatemyhomepage",method = RequestMethod.POST)
    public BaseResult updatemyhomepage(@RequestBody CmUser cmUser){
        return cmUserService.updatemyhomepage(cmUser);
    }
}
